package com.lyj.hw_student.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyj.hw_student.system.entity.SysRole;
import com.lyj.hw_student.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lyj
 * @since 2025-04-22
 */
@RestController
@RequestMapping("/sysRole")
public class SysRoleController {
    @Autowired
    ISysRoleService sysRoleService;
    @RequestMapping("/add")
    public String testRoleAdd( @RequestBody SysRole sysRole){
        //写自己需要的方法 （业务代码需要写到service层）
        sysRoleService.testRoleAdd(sysRole);
        return "success";
    }


    /*
     *分页查询角色列表
     * @param roleName String
     * @param current Integer
     * @param size Integer
     */
    @GetMapping("/pageRole")
    public  Page<SysRole> pageRole(@RequestParam(required = false) String roleName,
                        @RequestParam(required = false) Integer current,
                            @RequestParam(required = false)Integer size){
        if(current == null){
            current = 1;
        }
        if(size == null){
            size = 10;
        }
       //创建分页对象，将当前页和每页显示的数量传进去
        Page<SysRole> page = new Page<>(current,size);
        //调用service层方法，查询角色列表，将分页的对象和查询条件传进去
        Page<SysRole> sysRolePage = sysRoleService.queryRolePage(page,roleName);
        return sysRolePage;
    }


    @GetMapping("/{roleId}")
    public SysRole getRoleById(@PathVariable Integer roleId) {
        return sysRoleService.getById(roleId);
    }

    @PutMapping("/{roleId}")
    public String updateRole(@PathVariable Integer roleId, @RequestBody SysRole sysRole) {
        sysRole.setRoleId(roleId);
        sysRoleService.updateById(sysRole);
        return "success";
    }

    @DeleteMapping("/{roleId}")
    public String deleteRole(@PathVariable Integer roleId) {
        sysRoleService.removeById(roleId);
        return "success";
    }



}
